open(BUFF,"../chr8q24_emerge.bim");
while(<BUFF>)
{
	chomp($_);
	@a=split("\t",$_);
	$inp_mark{$a[1]}=1;
}
open(BUFF,"impute_8q24_out") or die " no file exists impute_8q24_out\n";
#open(WRBUFF2,">final_input") or die " not able to write the files final_impute_8q24_out\n"; 
open(BUFF1,"hapmap_impute_8q24_out") or die "no file exists hapmap_impute_8q24_out\n";
while(<BUFF>)
{
	chomp($_);
	@a=split(" ",$_);
	$impute{$a[1]}=1;
}
while(<BUFF1>)
{
	chomp($_);
	@a=split(" ",$_);
	$hapmap{$a[1]}=1;	
}
=head
open(BUFF,"../chr8q24_emerge.tped");
while(<BUFF>)
{
        chomp($_);
        @a=split(" ",$_);
	if(exists($inp_mark{$a[1]}) && exists($impute{$a[1]}) && $hapmap{$a[1]})
	{
		print WRBUFF2 $_."\n";
	} 
}

=cut
open(WRBUFF,">final_impute_8q24_out") or die " not able to write the files final_impute_8q24_out\n";
open(WRBUFF1,">final_hapmap_impute_8q24_out") or die "no file exists final_hapmap_impute_8q24_out\n";
open(BUFF,"impute_8q24_out") or die " no file exists impute_8q24_out\n";
open(BUFF1,"hapmap_impute_8q24_out") or die "no file exists hapmap_impute_8q24_out\n";
while(<BUFF>)
{
	chomp($_);
	@a=split(" ",$_);
	#if(exists($hapmap{$a[1]}) && exists($inp_mark{$a[1]}))
	if(exists($hapmap{$a[1]}))
	{
		print WRBUFF $_."\n";
	}
}
open(BUFF,"1kg_tfam");
$i=5;
while(<BUFF>)
{
        chomp($_);
        @a=split(" ",$_);
	$order{$a[1]}=$i++." ".$i++." ".$i++;
	#print $order{$a[1]}."\n";
}
open(BUFF,"updates_hapmap_tfam") or die " no file found updates_hapmap_tfam\n";
push(@input,0);
push(@input,1);
push(@input,2);
push(@input,3);
push(@input,4);
while(<BUFF>)
{
	chomp($_);
	@a=split(" ",$_);
	if(exists($order{$a[1]}))
	{
		@b=split(" ",$order{$a[1]});
		push(@input,$b[0]);
		push(@input,$b[1]);
		push(@input,$b[2]);
	}
}
open(BUFF1,"hapmap_impute_8q24_out") or die "no file exists hapmap_impute_8q24_out\n";
#print @input."\n";
while(<BUFF1>)
{
	chomp($_);
	@a=split(" ",$_);
	#if(exists($impute{$a[1]}) && exists($inp_mark{$a[1]}))
        if(exists($impute{$a[1]}))
	{
		print WRBUFF1 $a[$input[0]];
		for($i=1;$i<@input;$i++)
		{
			print WRBUFF1 " $a[$input[$i]]";
		}
		 print WRBUFF1 "\n";
        }
}
=head
open(BUFF,"hapmap_impute_8q24_out");
=head
$a1="0.97";
$a2="0.01";
$a3 =1-$a1-$a2;
printf("%.2f", $a3)."\n";
